<?php

	// Session
	session_start();
	
	// Nese vizitori eshte i identifikuar
	if (!isset($_SESSION['user_id'])) {
		header("Location: login.php");
	}
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	
	// Project
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/members/index.html');
	define('UID',$_SESSION['user_id']);
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	
	
	// Connection
	dbOpen("comunity");
	
	
	// Inicializimi i shablonave
	$tmp = new Template(TPL,$PROJECT['lang']);
	

	// INDEXING ARRAYS
	$STATUS = $tmp->indexing("status");
	$ACCESS = $tmp->indexing("access");
	$SECTION = indexing("SELECT id,name FROM sections");
	

	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../members"));
	$tmp->setContent("MYMENU",$tmp->menu("mymenu",FILENAME));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	
	
	// LABELS
	$tmp->setLabels();

	
	// PROFIL
	$USER = mysql_fetch_assoc(mysql_query("SELECT nickname,status,style,regdate,logout FROM users WHERE id='".UID."'"));
	$USER['publications'] = mysql_num_rows(mysql_query("Select id FROM library WHERE author_id='".UID."'"));
	$USER['countKendet'] = mysql_num_rows(mysql_query("Select id FROM comments WHERE user_id='".UID."'"));
	$USER['status'] = $STATUS[$USER['status']];
	$USER['regdate'] = setDate($USER['regdate']);
	
	dbOpen("library");
	$USER['countLibraria'] = mysql_num_rows(mysql_query("SELECT id FROM comments WHERE user_id = '".UID."'"));
	$USER['comments'] = $USER['countKendet'] + $USER['countLibraria'];
	dbOpen("comunity");

	// USER PROFIL
	$tmp->setContent("INFOUSER",$USER);
	

	// If user logout was not corect
	if ($USER['logout'] == 2) {
		
		// NOTIFICATION MESSAGE
		$NOTMSG = $tmp->xmlContent("logoutalert");
		$tmp->setContent(array("notTitle"=>"'$NOTMSG[title]'","notContent"=>$NOTMSG['content']));

		// Set logout value to 1
		mysql_query("UPDATE users SET logout = '1' WHERE id = '".UID."'");
	}	

	
	// Last Comments: Komentet e fundit te shkruara per librat e vizitorit
	dbOpen("comunity");
	
	$query = mysql_query("SELECT library.title, comments.user_id, comments.comment, comments.date
						  FROM library, comments 
						  WHERE library.author_id='".UID."' AND library.id = comments.book_id
						  ORDER BY comments.date DESC
						  LIMIT 5
						");
	
	// Nese gjen ndonje koment
	if (mysql_num_rows($query) > 0) {

		// Per cdo resht te gjetur
		while ($row = mysql_fetch_assoc($query)) {
			
			// User Name
			$row['user'] = implode(mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE id='".$row['user_id']."'")));
			
			// Fushha asociative per zevendesimin e vlerave per bllokun e komenteve te fundit
			$COMMENTS[] = array("user" => $row['user'], 
								"date" => setDate($row['date']), 
								"title" => $row['title'], 
								"comment" => $row['comment']
								);
		}
	}
	// Nese nuk gjen asnje koment	
	else 
		// Rikthej fushen boshe per largimin e bllokut te komenteve te fundit
		$COMMENTS = array();
		
	
	// Zevendeso vlerat per blokun COMMENTS
	$tmp->setContent("COMMENTS",$COMMENTS);
	
	
	// Publikimet e fundit
	$query = mysql_query("SELECT id,sid,volume,title,counter,access,pubdate
						  FROM library 
						  WHERE author_id='".UID."'
						  ORDER BY pubdate DESC
						  LIMIT 5
						 ");
	

	// Nese gjen ndonje resht
	if (mysql_num_rows($query) > 0) {
		
		while ($row = mysql_fetch_assoc($query)) {
			
			// Nese Libri nuk eshte pjese e ndonje permledhjeje
			// Atehere zevendeso vendet e titullit dhe Vellimit
			if (empty($row['volume'])) {
				
				$row['volume'] = $row['title'];
				$row['title'] = null;
			}
			
			// Numero komentet
			$row['comments'] = mysql_num_rows(mysql_query("SELECT id FROM comments WHERE book_id='".$row['id']."'"));
			
			// Book access
			$row['access'] = $ACCESS[$row['access']];
			
			// Publication date
			$row['pubdate'] = setDate($row['pubdate']);
			
			// Section name
			$row['section'] = $SECTION[$row['sid']];
			
			// Ngjyrosja e cdo te dyti resht
			$row['bgcolor'] = ($i % 2) ? "#ece1cd" : "#efebe3";
			
			// Set BOOK ARRAY
			$BOOK[] =  $row;
			
			$i++;
		}
		
		// Rikthej fushen boshe per largimin e bllokut te porosis
		$MESSAGE = array();
	}
	// Nese nuk gjen asnje resht
	else {
		// Rikthej fushen boshe per largimin e bllokut te publikimeve te fundit
		$BOOK = array();
		
		// Krijo bllokun e porosis
		$MESSAGE = $tmp->xmlContent('nobooks');
	}

	// Blloku u porosis ne raste se nuk kemi ndonje publikacion
	$tmp->setContent("MESSAGE",$MESSAGE);
	
	// Zevendeso vlerat per bllokun BOOKS
	$tmp->setContent("BOOKS",$BOOK);
	
	// Dhe cdo gje ka nje fund, trego faqen						  
	echo $tmp->vorlage;
?>